in this article we know how to connect MSSQL database table to read table value wincc hmi. to connect MSSQL database first connect database than read value of table. to do this first create a data base in same database create a table. second create a DSN to connect MSSQL database to VBSCRIPT. it is very simple to read and write value using vbscript in wincc HMI. VBScript have so many features. To read values from an MSSQL table using VBScript, you can use ADO (ActiveX Data Objects). in this article have simple VBscript help you connect to an MSSQL database, execute a query, and read table values.
Microsoft SQL Server Management Studio (SSMS) is an integrated environment for managing, configuring, and interacting with SQL Server instances. It is a powerful tool provided by Microsoft for database administrators (DBAs), developers, and analysts to manage databases, write queries, and perform administrative tasks. SSMS is free and can be downloaded from the official Microsoft website.
first step to create tag database to store tag value so we can read and write these tag value. a tag database an organized collection of tag values that can be easily accessed, managed, and updated. You can create database by Using the GUI (Graphical User Interface) or Using SQL Query. follow these steps to create a database in MSSQL. Using the GUI (Graphical User Interface):
tagdatabase
).
"CREATE DATABASE tagdatabase2;"
A table in a database is a structured format for storing data. It organizes data into rows and columns, making it easy to manage, retrieve, and manipulate. Tables are a fundamental component of relational databases. you can create table in mssql by using GUI or SQL query Method 1: Using the GUI (Graphical User Interface)
INT
, NVARCHAR(50)
), and constraints (e.g., NOT NULL
, PRIMARY KEY
).
Users
table:
USE tagdatabase2; -- Switch to the database
CREATE TABLE tagtable (
id INT PRIMARY KEY, -- Unique identifier
tagname NVARCHAR(50) NOT NULL, -- Tag name
tagvalue INT NOT NULL, -- Tag value
JoinDate DATE DEFAULT GETDATE() -- Default to current date
);
after creating table add following value to tagtable
right click on tag table and then select edit top 200 rows
add following value to table
or execute following sql query directly to insert value in tagtable
A Data Source Name (DSN) is a configuration that provides the necessary information for an application to connect to a database, such as the database driver, server address, and authentication details. DSN commonly stands for Data Source Name, a configuration used in computing to define how an application connects to a database. Step 1: Open ODBC Data Source Administrator
Step 2: Add a New DSN
select driver for DSN
MySql
).Step 4: Configure Authentication
Step 5: Configure the Default Database
Step 6: Test the DSN
open tia portal and create a new project enter details of project or add hmi in your project. in this example we have a button and two text field. when button is pressed value of sql table is show in text field. add a script and rename to read_sql table. Assign vbscript to button1 press event property. when button is pressed value of selected table column show in text field. if any error comes during connection of read value it shows on text field.
example of vbscript to read MSSQL Database table value in Siemens Wincc HMI This script demonstrates how to connect to a database using ODBC, execute an SQL query, and display the results in a human-machine interface (HMI).
conn
: The database connection object.strConn
: The connection string for the ODBC Data Source Name (DSN).sqlSelect
: The SQL query to retrieve the desired value.dsn
: The ODBC DSN name (e.g., "MySql").valueRead
: Holds the value retrieved from the database.textfield1
and textfield2
: References to text fields in the HMI runtime.HmiRuntime.Screens
object:
Set textfield1 = HmiRuntime.Screens("Screen_1").ScreenItems("Text field_1") Set textfield2 = HmiRuntime.Screens("Screen_1").ScreenItems("Text field_2")These text fields are used to display the results of the script.
dsn = "MySql" strConn = "DSN=" & dsn & ";Trusted_Connection=Yes;"The
Trusted_Connection=Yes
setting enables Windows Authentication.
tagname = 'temprature1'
:
sqlSelect = "SELECT tagvalue FROM tagtable WHERE tagname = 'temprature1'"
The script creates and opens the connection using the ADO object:
Set conn = CreateObject("ADODB.Connection") conn.Open strConnError handling is enabled using
On Error Resume Next
.
Set rs = conn.Execute(sqlSelect) If Not rs.EOF Then valueRead = rs.Fields("tagvalue").Value textfield1.Text = "Value retrieved: " & valueRead Else textfield2.Text = "No record found for tagname 'temprature1'." End IfIf the query returns a result, the value is displayed in
textfield1
. Otherwise, an error message is shown in textfield2
.
textfield2.Text = "Failed to connect to the database."All resources (recordset and connection) are closed and cleaned up at the end:
If conn.State = 1 Then conn.Close Set conn = Nothing
MySql
, tagtable
) with actual database and table details.